package com.hosp.controller.v1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Chargeproject;
import com.hosp.controller.dto.BeHosiptalDTO;
import com.hosp.service.BeHospitalService;
import com.hosp.utils.ExcelUtil2;
import com.hosp.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 住院办理 API
 *
 * @author bing.zhang06@hand-china.com 2021/10/12 22:44
 **/
@Slf4j
@RestController
@RequestMapping("/be-hosipital")
public class BeHospitalController {
    @Autowired
    private BeHospitalService beHospitalService;

    /**
     * 查询列表
     *
     * @param beHosiptalDTO 查询条件
     * @param pageRequest   分页条件
     * @return 响应
     */
    @GetMapping("/list")
    public ResultVO<BeHospitalVO> list(BeHosiptalDTO beHosiptalDTO, PageRequestVO pageRequest) {
        log.debug("input is {}.", beHosiptalDTO);
        PageInfo<BeHospitalVO> pageInfo = beHospitalService.list(beHosiptalDTO, pageRequest);
        return new ResultVO<>(pageInfo);
    }

    /**
     * 主键查询
     *
     * @param behId 查询条件
     * @return 响应
     */
    @GetMapping("/{behId}")
    public ResultVO<BeHospitalVO> selectById(@PathVariable("behId") Integer behId) {
        BeHospitalVO beHospitalVO = beHospitalService.selectById(behId);
        return new ResultVO<>(beHospitalVO);
    }

    /**
     * 住院人详细信息查询(包含收费项目)
     *
     * @param behId 查询条件
     * @return 响应
     */
    @GetMapping("/detail")
    public ResultVO<BeHospitalDetailVO> detail(Integer behId, PageRequestVO pageRequest) {
        PageInfo<BeHospitalDetailVO> detailVOList = beHospitalService.detail(behId, pageRequest);
        return new ResultVO<>(detailVOList);
    }


    /**
     * 住院人详细信息查询(包含收费项目) 导出
     *
     * @param behId 查询条件
     * @return 响应
     */
    @PostMapping("/detail/export")
    public void detailExport(Integer behId, PageRequestVO pageRequest, HttpServletResponse response) {
        PageInfo<BeHospitalDetailVO> detailVOList = beHospitalService.detail(behId, pageRequest);
        List<BeHospitalDetailVO> list = detailVOList.getList();
        List<ChargeProjectExportVO> exportVOList = new ArrayList<>();

        for (BeHospitalDetailVO detailVO : list) {
            ChargeProjectExportVO projectExportVO = new ChargeProjectExportVO();
            projectExportVO.setBehId(detailVO.getBehId());
            if (Objects.nonNull(detailVO.getReg())) {
                projectExportVO.setHosrName(detailVO.getReg().getHosrName());
            }
            Chargeproject chargeproject = detailVO.getChargeproject();
            if (Objects.nonNull(chargeproject)) {
                projectExportVO.setChapName(chargeproject.getChapName());
                projectExportVO.setChapMoney(chargeproject.getChapMoney());
                projectExportVO.setChapTime(chargeproject.getChapTime());
            }
            exportVOList.add(projectExportVO);
        }
        log.debug("exportVOList is {}.", exportVOList);

        try {
            ExcelUtil2.exportExcel(response, "data-export", "测试",
                    ChargeProjectExportVO.class, exportVOList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 住院结算基础信息查询(包含收费项目+药品费用)
     *
     * @return 响应
     */
    @GetMapping("/account-basic")
    public ResultVO<BeHospitalDetailVO> accountBasic(PageRequestVO pageRequest) {
        PageInfo<BeHospitalDetailVO> detailVOList = beHospitalService.accountBasic(pageRequest);
        return new ResultVO<>(detailVOList);
    }

    /**
     * 住院结算详细信息查询(包含收费项目+药品费用)
     *
     * @param behId 查询条件
     * @return 响应
     */
    @GetMapping("/account-detail")
    public ResultVO<BeHospitalDetailVO> accountDetail(Integer behId, PageRequestVO pageRequest) {
        PageInfo<BeHospitalDetailVO> detailVOList = beHospitalService.accountDetail(behId, pageRequest);
        return new ResultVO<>(detailVOList);
    }

    /**
     * 保存
     *
     * @param beHosiptalDTO 保存对象
     * @return 响应
     */
    @PostMapping("/save")
    public ResultVO<String> save(BeHosiptalDTO beHosiptalDTO) {
        log.debug("BeHosiptalDTO is {}.", beHosiptalDTO);

        boolean flag = beHospitalService.save(beHosiptalDTO);

        if (flag) {
            return new ResultVO<>(200, "保存成功");
        } else {
            return new ResultVO<>(500, "保存失败");
        }
    }

    /**
     * 保存
     *
     * @param beHosiptalDTO 保存对象
     * @return 响应
     */
    @PostMapping("/cp-save")
    public ResultVO<String> cpSave(BeHosiptalDTO beHosiptalDTO) {
        log.debug("BeHosiptalDTO is {}.", beHosiptalDTO);

        boolean flag = beHospitalService.cpSave(beHosiptalDTO);

        if (flag) {
            return new ResultVO<>(200, "保存成功");
        } else {
            return new ResultVO<>(500, "保存失败");
        }
    }
}
